﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.SqlClient;
using System.Net;
using System.Data;

namespace AutoRequest
{
    public static class Common
    {
        private static int idx = 1;
        private static string[] recruiter = new string[]
		{
			"1768050400@qq.com", 
			"1617047154@qq.com", 
			"1274584051@qq.com"
		};
        public static string DoRequest(string url)
        {
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.UserAgent = "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";
            httpWebRequest.Accept = "*/*";
            httpWebRequest.KeepAlive = true;
            httpWebRequest.Headers.Add("Accept-Language", "zh-cn,en-us;q=0.5");
            HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            Stream responseStream = httpWebResponse.GetResponseStream();
            string result;
            using (StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("utf-8")))
            {
                string text = streamReader.ReadToEnd();
                text = text.Replace("\r", "").Replace("\n", "");
                result = text;
            }
            return result;
        }
        public static DateTime ToDate(this string date)
        {
            DateTime now = DateTime.Now;
            if (DateTime.TryParse(date, out now))
            {
                return now;
            }
            return DateTime.Now;
        }
        public static void RecordToDB(ref int count, List<Info> data)
        {
            using (SqlConnection sqlConnection = new SqlConnection("Data Source=Sql1001.webweb.com,2433;Initial Catalog=DB_98EBF3_ueEngine;User ID=DB_98EBF3_ueEngine_admin;Password=ueEngine2012;Asynchronous Processing=true"))
            {
                sqlConnection.Open();
                foreach (Info current in data)
                {
                    try
                    {
                        SqlCommand sqlCommand = sqlConnection.CreateCommand();
                        sqlCommand.CommandText = string.Format("insert into SourceData values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}')", new object[]
						{
							current.orgId, 
							current.Caption, 
							current.PublishDate.ToShortDateString(), 
							current.ContactMan, 
							current.ContactPhone, 
							current.Area, 
							current.Address, 
							current.PositionType, 
							current.PositionName, 
							current.Vacancy, 
							current.Salry, 
							current.Period, 
							current.Experience, 
							current.Degree, 
							current.PositionDescript, 
							current.CompanyName, 
							current.CopBusiness, 
							current.CopType, 
							current.CopAbout, 
							current.CopDimensions
						});
                        sqlCommand.CommandType = CommandType.Text;
                        int num = sqlCommand.ExecuteNonQuery();
                        if (num == 1)
                        {
                            Console.WriteLine("还剩" + --count + "条数据等待写入");
                        }
                        sqlCommand.Dispose();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(string.Concat(new object[]
						{
							"写入第：", 
							--count, 
							"数据时出现异常", 
							Environment.NewLine, 
							ex.Message
						}));
                        using (StreamWriter streamWriter = new StreamWriter(AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "writeToDbErr.txt"))
                        {
                            streamWriter.WriteLine(ex.ToString());
                        }
                    }
                }
            }
        }
        public static string GetRecruiter()
        {
            int arg_18_0 = Common.idx = ((Common.idx == Common.recruiter.Length) ? 0 : (Common.idx + 1));
            return Common.recruiter[Common.idx % Common.recruiter.Length];
        }
    }
}
